// 引入 http 核心模块
const http = require('http')
// 引入 fs 核心模块
const fs = require('fs')

// 创建 webserver
const server = http.createServer((request, response) => {
  // 解构赋值
  const { url, method } = request
  console.log('请求资源：', url, '，请求方法：', method)

  let html = '<h1>Hello Server</h1>'
  /* 路由 */
  switch(url) {
    case '/index.html':
      html = fs.readFileSync('./public/index.html')
    case '/html/login.html':
      html = fs.readFileSync('./public/html/login.html')
  }

  // 响应
  response.end(html)
})

// 监听端口，等待浏览器访问
server.listen(8888, () => console.log('Server running at http://localhost:8888'))
